layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * 支付账单管理
     */
    var Payment = {
        tableId: "paymentTable"
    };

    /**
     * 初始化表格的列
     */
    Payment.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id',hide:'true', title: 'id',},
            {field: 'playerId', sort: true,align:'center', title: '购买人ID',totalRowText:'当前页总计'},
            {field: 'nickname', sort: true,align:'center', title: '购买人',},
            {field: 'orderNo', sort: true,align:'center', title: '订单号'},
            {field: 'orderId', sort: true,hide:'true',align:'center',  title: '订单id'},
            {field: 'transactionId', sort: true,align:'center', title: '微信支付编号'},
            {field: 'timeStamp', sort: true,align:'center',  hide:'true',title: '微信支付的时间戳'},
            {field: 'nonceStr', sort: true, hide:'true',align:'center', title: '微信支付的随机字符串'},
            {field: 'orderPayPrice', sort: true,align:'center', title: '支付金额(元)',totalRow:'true',
                templet:function (d) {
                    return Feng.formatCurrency(d.orderPayPrice);
                }
            },
            {field: 'payType', sort: true, align:'center',title: '支付方式'},
            {field: 'payStatusName', sort: true,align:'center', title: '支付状态'},
            {field: 'payTime', sort: true,align:'center', title: '支付时间'},
            {field: 'createTime', sort: true,align:'center', title: '创建时间'},
         /*   {align: 'center', toolbar: '#tableBar', title: '操作'}*/
        ]];
    };


    var payTimeMax = null
    var payTimeMin = null
    var totalPriceMin = null
    var totalPriceMax = null
    /**
     * 点击查询按钮
     */
    Payment.search = function () {
        var queryData = {};
        queryData['orderNo'] = $("#orderNo").val();
        queryData['trxid'] = $("#trxid").val();
        queryData['playerId'] = $("#playerId").val();
        queryData['payTimeMax'] = $("#payTimeMax").val();
        queryData['payTimeMin'] = $("#payTimeMin").val();
        queryData['totalPriceMin'] = $("#totalPriceMin").val();
        queryData['totalPriceMax'] = $("#totalPriceMax").val();
        table.reload(Payment.tableId, {where: queryData
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
    handleData=function ( ) {
        payTimeMax = $("#payTimeMax").val();
        payTimeMin = $("#payTimeMin").val();
        totalPriceMin = $("#totalPriceMin").val();
        totalPriceMax = $("#totalPriceMax").val();
        if(!((payTimeMax==""&&payTimeMin=="")||(payTimeMax!="" && payTimeMin!=""))){
            if(payTimeMin==""){
                payTimeMin="2000-01-01"
            }
            if(payTimeMax==""){
                payTimeMax=Feng.currentDate();
            }
        }
        if(!((totalPriceMin==""&&totalPriceMax=="")||(totalPriceMax!="" && totalPriceMin!=""))){
            if(totalPriceMin==""){
                totalPriceMin=0
            }
            if(totalPriceMax==""){
                totalPriceMax=1000000000;
            }
        }
    }

    laydate.render({
        elem: '#payTimeMin',
        max: Feng.currentDate()
    });

    laydate.render({
        elem: '#payTimeMax',
        max: Feng.currentDate()
    });


    /**
     * 弹出添加对话框
     */
    Payment.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加支付账单',
            content: Feng.ctxPath + '/payment/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Payment.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Payment.exportExcel = function () {
        var checkRows = table.checkStatus(Payment.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Payment.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改支付账单',
            content: Feng.ctxPath + '/payment/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Payment.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Payment.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/payment/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Payment.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Payment.tableId,
        url: Feng.ctxPath + '/payment/list',
        page: true,
        totalRow:'true',
        height: "full-158",


        cellMinWidth: 100,
        cols: Payment.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Payment.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Payment.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Payment.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Payment.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Payment.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Payment.onDeleteItem(data);
        }
    });
});
